package com.anlogic.sdk.ui.tests.utils;

import java.util.regex.Pattern;

import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.eclipse.finder.waits.Conditions;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
import org.eclipse.swtbot.swt.finder.SWTBot;
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
import org.eclipse.swtbot.swt.finder.waits.ICondition;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotButton;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotCheckBox;


public class ProgramFlashUtils {


	public static void programFlash(SWTWorkbenchBot bot, String chip, String connection, String imageFile,
			String offset, String flashType, boolean customize, boolean EnablePjtag, boolean Erase, boolean XIPMode,
			boolean Verify) {

		bot.waitUntil(Conditions.shellIsActive("data - FutureDynasty"), 90000);

		bot.waitUntil(new DefaultCondition() {
		    @Override
		    public boolean test() throws Exception {
		        try {
		            return bot.menu("Tools").menu("Program Flash").isEnabled();
		        } catch (WidgetNotFoundException e) {
		            return false;
		        }
		    }

		    @Override
		    public String getFailureMessage() {
		        return "Menu 'Tools > Program Flash' did not appear or is not enabled within the timeout period.";
		    }
		}, 30*1000);

		bot.menu("Tools").menu("Program Flash").click();

		bot.waitUntil(Conditions.widgetIsEnabled(bot.comboBox(0)), 10_000);
		bot.comboBox(0).setSelection(chip);

		bot.waitUntil(Conditions.widgetIsEnabled(bot.comboBox(1)), 10_000);
		bot.comboBox(1).setSelection(connection);

		bot.waitUntil(Conditions.widgetIsEnabled(bot.textWithLabel("Image File:")), 10_000);
		bot.textWithLabel("Image File:").setText(imageFile);

		bot.textWithLabel("Offset:").setText(offset);

		bot.waitUntil(Conditions.widgetIsEnabled(bot.comboBox(2)), 10_000);
		bot.comboBox(2).setSelection(flashType);

		if (customize) {
			SWTBotCheckBox customizeBox = bot.checkBox("Use Customize Flash");
			bot.waitUntil(Conditions.widgetIsEnabled(customizeBox), 10_000);
			if (!customizeBox.isChecked()) {
				customizeBox.click();
			}

			bot.textWithLabel("bankId:").setText("0");
			bot.textWithLabel("readCmd:").setText("0x03");
			bot.textWithLabel("pprogCmd:").setText("0x02");
			bot.textWithLabel("eraseCmd:").setText("0xd8");
			bot.textWithLabel("pageSize:").setText("0x100");
			bot.textWithLabel("sectorSize:").setText("0x1000");
			bot.textWithLabel("chipSize:").setText("0x4000000");
		}

		if (EnablePjtag) {
			SWTBotCheckBox pjtagBox = bot.checkBox("Enable Pjtag");
			bot.waitUntil(Conditions.widgetIsEnabled(pjtagBox), 10_000);
			if (!pjtagBox.isChecked()) {
				pjtagBox.click();
			}
		}

		if (Erase) {
			SWTBotCheckBox eraseBox = bot.checkBox("Erase the whole flash");
			bot.waitUntil(Conditions.widgetIsEnabled(eraseBox), 10_000);
			if (!eraseBox.isChecked()) {
				eraseBox.click();
			}
		}

		if (XIPMode) {
			SWTBotCheckBox xipBox = bot.checkBox("Enable XIP Mode");
			bot.waitUntil(Conditions.widgetIsEnabled(xipBox), 10_000);
			if (!xipBox.isChecked()) {
				xipBox.click();
			}
		}

		if (Verify) {
			SWTBotCheckBox verifyBox = bot.checkBox("Verify after flash");
			bot.waitUntil(Conditions.widgetIsEnabled(verifyBox), 10_000);
			if (!verifyBox.isChecked()) {
				verifyBox.click();
			}
		}


		SWTBotButton programButton = bot.button("Program");
		bot.waitUntil(Conditions.widgetIsEnabled(programButton), 10_000);
		programButton.click();



		bot.viewByTitle("Console (Download Tool)").show();

		SWTBotView consoleView = bot.viewByPartName("Console");
		consoleView.setFocus();

		if (consoleView.isActive()) {
			Pattern p = Pattern.compile(".*Programming Finished.*", Pattern.DOTALL);
			bot.waitUntil(consoleTextMatches(consoleView, p), 120_000); // 最多等两分钟
		}
	}

	protected static ICondition consoleTextMatches(SWTBotView consoleView, Pattern pattern) {
		return new ConsoleTextMatches(consoleView, pattern);
	}

	static class ConsoleTextMatches implements ICondition {
		private final SWTBotView view;
		private Pattern pattern;

		public ConsoleTextMatches(SWTBotView view, Pattern pattern) {
			this.view = view;
			this.pattern = pattern;
		}

		@Override
		public boolean test() {
			if (view.isActive()) {
				String output = view.bot().styledText().getText();
				java.util.regex.Matcher m = pattern.matcher(output);
				return m.matches();
			}
			return false;
		}

		@Override
		public void init(SWTBot bot) {
		}

		@Override
		public String getFailureMessage() {
			return "flash error";
		}
	}

}
